package cn.qy.kotlin.base

/**
 * 异常
 */
fun main() {
    var swordsJuggling: Int? = null
    val isJugglingProficient = (1..3).shuffled().last() == 3
    if (isJugglingProficient) {
        swordsJuggling = 2
    }
    try {
        proficiencyCheck(swordsJuggling)
        swordsJuggling!!.plus(1)
    } catch (e: Exception) {
        println(e)
    }
    proficiencyCheck2(swordsJuggling)
    println("You juggle $swordsJuggling swords!")
}

// 判断若为空则手动抛异常
fun proficiencyCheck(swordsJuggling: Int?) {
    swordsJuggling ?: throw UnskilledSwordJugglerException()
}

// 先决条件函数
fun proficiencyCheck2(swordsJuggling: Int?) {
    checkNotNull(swordsJuggling) { "com.bignerdranch.nyethack.Player cannot juggle swords" }
    requireNotNull(swordsJuggling) { "com.bignerdranch.nyethack.Player cannot juggle swords" }
}

// 自定义异常
class UnskilledSwordJugglerException() : IllegalStateException("com.bignerdranch.nyethack.Player cannot juggle swords")

